<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            padding-bottom: 800px;
            padding-left: 30px;
        }
        .cbox{
            border: 1px solid #999;
            padding: 20px;
            margin: 10px 0px;
        }
    </style>
</head>
<body>
    <div id="app" class="cbox">
        <h4>根组件</h4>
        <pre>
        中央事件总线的实现原理
            在全局环境提供一个方法变量，由接收数据的组件完成该方法变量的方法覆盖，
            提供数据的组件完成该全局方法的触发，通过实参和形参对应关系实现跨组件的数据传递
        </pre>
        <comp-a></comp-a>
        <comp-b></comp-b>
    </div>
    <script type="module">
        import { createApp } from "../../assets/vue3/vue.esm-browser.js"

        let updateMsg = function(){}

        const CompA = {
            template:`
                <div class="cbox">
                    <h4>组件A</h4>
                    <p>msg:{{ msg }}</p>
                    <input type="button" value="触发全局方法" @click="sendMsg()">
                </div>
            `,
            data() {
                return {
                    msg: "组件A-msg"
                }
            },
            methods: {
                sendMsg() {
                    updateMsg(this.msg)
                }
            },
        }
        const CompB = {
            template:`
                <div class="cbox">
                    <h4>组件B</h4>
                    <p>msg:{{ msg }}</p>
                    <input type="button" value="覆盖全局方法" @click="setUpateMsg()">
                </div>
            `,
            data() {
                return {
                    msg: ""
                }
            },
            methods: {
                setUpateMsg() {
                    updateMsg = (arg)=>{
                        this.msg = arg;
                    }
                }
            },
        }
        createApp({
            components: {
                CompA,CompB
            },
            data(){
                return {
                    
                }
            }
        }).mount("#app")
    </script>
</body>
</html>